package com.littlec.sdk.asynctask;

import java.io.File;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;

import com.littlec.sdk.XMPPConnectionManager;
import com.littlec.sdk.entity.CMMember;
import com.littlec.sdk.entity.CMMessage;
import com.littlec.sdk.entity.messagebody.LocationMessageBody;
import com.littlec.sdk.manager.CMGroupManager;
import com.littlec.sdk.manager.CMMessageManager;
import com.littlec.sdk.utils.CMChatGlobalStorage;
import com.littlec.sdk.utils.CMChatListener;
import com.yinhai.constant.Contact;
import com.yinhai.constant.UrlConstant;

import android.os.AsyncTask;

public class GroupChatFileUploadPosAsyncTask extends AsyncTask<File, Integer, Boolean>{
	private CMMessage message;
	private String resultJson;

	public GroupChatFileUploadPosAsyncTask(CMMessage message) {
		this.message = message;
	}

	protected void onPreExecute() {
	}

	protected Boolean doInBackground(File[] params) {
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost post3 = new HttpPost(UrlConstant.LOCAL_URI
				+ "addPhoto?phone=" + message.getUuid());
		File upfile = params[0];
		MultipartEntity entity3 = new MultipartEntity();
		try {
			entity3.addPart("oicon", new FileBody(upfile));
			post3.setEntity(entity3);
			HttpResponse rsp = httpclient.execute(post3);
			HttpEntity entity = rsp.getEntity();
			if (entity != null) {
				entity.consumeContent();
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return Boolean.valueOf(true);
	}

	protected void onProgressUpdate(Integer[] integer) {
		int progress = integer[0].intValue();
		if (progress != this.message.getProgress()) {
			this.message.setProgress(progress);
		}
	}

	protected void onPostExecute(Boolean result) {
		if (result.booleanValue()) {
			try {
				
				String groupId = message.getTo();
				String groupName = CMGroupManager.getInstance()
						.getGroupNamefromServer(groupId);
				List<CMMember> members = CMGroupManager.getInstance()
						.getGroupMembersfromServer(groupId);
				for (CMMember member : members) {
					if (!CMChatGlobalStorage.getInstance().getmLoginUserName()
							.equals(member.getMemberId())) {
						String to = member.getMemberId();
						Chat chat = (Chat) CMMessageManager.getInstance().chats
								.get(to);
						XMPPConnection connection = XMPPConnectionManager
								.getInstance().getConnection();
						if (connection == null) {
							return;
						}
						if (chat == null) {
							chat = XMPPConnectionManager
									.getInstance()
									.getConnection()
									.getChatManager()
									.createChat(
											message.getTo()
													+ "@"
													+ XMPPConnectionManager
															.getInstance()
															.getConnection()
															.getServiceName(), null);

							CMMessageManager.getInstance().chats.put(to, chat);
						}
						if ((connection.isConnected())
								&& (connection.isAuthenticated())) {
							try {
								XMPPConnectionManager.getInstance()
										.sendOnlinePacket();
								LocationMessageBody messageBody = (LocationMessageBody) message
										.getMessageBody();
								Message messageChat = new Message();
								messageChat.setProperty("type", Contact.POSMAP);
								messageChat.setProperty("groupID", groupId);
								messageChat.setProperty("groupName", groupName);
								//群组聊天
								messageChat.setProperty("isGroup", "1");
								messageChat.setProperty("msgModelId", message.getUuid());
								messageChat.setProperty("latitude", messageBody.getLatitude()+"");
								messageChat.setProperty("longitude",messageBody.getLongitude()+"");
								messageChat.setProperty("sendName", CMChatGlobalStorage.getInstance().getmLoginUserRealName());
								messageChat.setBody(messageBody.getAddress());
								chat.sendMessage(messageChat);
								try {
									message.setProgress(100);
								} catch (Exception e) {
									e.printStackTrace();
								}
								return;
							} catch (Exception e) {
								e.printStackTrace();
							}

						}
					}
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}